using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace SEO.DBSqlHelp
{

    /// <summary>
    /// 版权所有: 版权所有(C) 2011，云里网络科技
    /// 内容摘要: DBHelper
    /// 完成日期：2011年4月30日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class DBHelper
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        private static SqlConnection cn;
        private static string strServerConn = "server=baidu.上海云里网络科技有限公司.com;uid=lilezhong;pwd=llz!@#53344521;database=EDM;pooling=true";//服务器

        /// <summary
        /// 连接投资卫士数据库
        /// </summary>
        /// <returns>返回投资卫士数据库连接字符串</returns>
        public static string ConnectionTouZiWeiShi()
        {
            return strServerConn;
        }

        /// <summary>
        /// 连接牛市网数据库
        /// </summary>
        /// <returns>返回牛市网数据库连接字符串</returns>
        public static string ConnectionSEO()
        {
            return "server=222.73.67.161,6626;uid=shrunwo;pwd=runwo!@#53344521;database=SEO;pooling=true";
        }

        public string ConnecitonSEO(string strPath)
        {
            return strServerConn;
            //return @"Data Source=.\SQLEXPRESS;AttachDbFileName=E:\\TouZiWeiShi.mdf;Integrated Security=True;User Instance=True;Asynchronous Processing=true";
        }

        /// <summary>
        /// 连接数据库
        /// <example>
        /// ConnectionSQL如：server=127.0.0.1;uid=test;pwd=123456;database=TestDB;pooling=true
        /// </example>
        /// </summary>
        /// <returns>返回SqlConnection实例</returns>
        public static SqlConnection GetCon()
        {
            if (cn == null)
            {
                cn = new SqlConnection(strServerConn);
            }
            return cn;
        }

       /// <summary>
       /// 执行数据库操作语句
       /// </summary>
       /// <param name="strSql">SQL语句</param>
       /// <returns>返回影响行数</returns>
        public static int ExecuteSql(string strSql)
        {
            int iNum = -1;
            SqlConnection cn = DBHelper.GetCon();
            SqlCommand cm = new SqlCommand(strSql, cn);
            try
            {
                cn.Open();
                iNum = cm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("执行数据库操作语句 -> " + ex.Message);
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                {
                    cn.Close();
                }
            }
            return iNum;
        }

        /// <summary>
        /// 函数输入SQL语句，输出一个结果集（DATATABLE）
        /// </summary>
        /// <param name="strSql">SQL语句</param>
        /// <returns>返回 DataTable</returns>
        public static DataTable GetTable(string strSql)
        {
            DataTable dt = new DataTable();
            SqlConnection cn = DBHelper.GetCon();
            try
            {
                cn.Open();
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataAdapter da = new SqlDataAdapter(cm);
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("函数输入SQL语句，输出一个结果集（DATATABLE） -> " + ex.Message);
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                {
                    cn.Close();
                }
            }
            return dt;
        }

        /// <summary>
        /// 函数输入SQL语句，返回一个SqlDataReader结果集
        /// </summary>
        /// <param name="strSql">SQL语句</param>
        /// <returns>返回 SqlDataReader 实例</returns>
        public static SqlDataReader ExReader(string strSql)
        {
            SqlConnection cn = DBHelper.GetCon();
            SqlDataReader dr = null;
            try
            {
                cn.Open();
                SqlCommand cm = new SqlCommand(strSql, cn);
                dr = cm.ExecuteReader(CommandBehavior.CloseConnection);

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("函数输入SQL语句，返回一个SqlDataReader结果集 -> " + ex.Message);
            }

            return dr;
        }
    }
}
